<?php
class FriendController extends Zend_Controller_Action
{
	
    public function init()
    {
        /* Initialize action controller here */
    	Zend_Loader::loadClass('UserModel');
    	Zend_Loader::loadClass('WallModel');
    	Zend_Loader::loadClass('FriendModel');
		Zend_Loader::loadClass('FriendrequestModel');
    }
    
    function preDispatch() {
    	$auth = Zend_Auth::getInstance();
    	if (!$auth->getIdentity()->id) $this->_redirect('/');
    	else {
    		$this->viewer = $auth->getIdentity()->username;
    		$this->viewer_id = $auth->getIdentity()->id;
    		$username = $this->_request->getParam('username');
			$this->info_username = UserModel::getuserinfo($username);
			$this->info_viewer = UserModel::getuserinfo($this->viewer);
			$this->list_friend = FriendModel::listfriend($this->info_username['id']);
			$this->view->info_username = $this->info_username;
			$this->view->info_viewer = $this->info_viewer;
			$this->view->list_friend = $this->list_friend;
			$this->view->controller = $this->_request->getParam('controller');
			$this->view->action = $this->_request->getParam('action');
			$this->view->isfriend = FriendModel::isfriend($this->info_username['id'],$this->info_viewer['id']);
			$this->view->requesting = FriendrequestModel::requesting($this->info_viewer['id'],$this->info_username['id']);
			$this->view->requested = FriendrequestModel::requesting($this->info_username['id'],$this->info_viewer['id']);
			$layout = $this->_helper->layout();
			$layout->setLayout('wall_layout');
			$this->view->headScript()->appendFile('/js/friend.js');
    		$this->view->headLink()->appendStylesheet('/css/friend.css');
    	}
    }
	
    public function indexAction(){
//		$this->view->avatar_path = str_replace('\application\controllers','\public\avatar',dirname(__FILE__));
//    	echo dirname(__FILE__);
    }
    
    public function fmkAction(){
    	$isfriend = array();
    	$people = UserModel::listall();
    	$count = 0;
    	foreach ($people as $fmk){
    		if ((FriendModel::isfriend($this->info_username['id'],$fmk['id']))||($fmk['id']==$this->info_username['id']))
    					$isfriend[$fmk['id']] = 1;
 	   		else{
 	   			$dm[$count]['id'] = $fmk['id'];
 	   			$dm[$count]['mutual'] = FriendModel::mutualfriend($this->info_username['id'],$fmk['id']);
 	   			$dm[$count]['hobby'] = UserModel::countsamehobby($this->info_username['id'],$fmk['id']);
 	   			$dm[$count]['number_request'] = FriendrequestModel::getnumberrequest($this->info_username['id'],$fmk['id']);
 	   			$dm[$count]['rate'] = 1000+$dm[$count]['mutual']*50 + $dm[$count]['hobby']['count']*100 - $dm[$count]['number_request']*30;
 	   			$count++;
 	   		}
    	}
//		print_r($dm);
    	for($i=0;$i<$count;$i++){
    		for($j=$i;$j<$count;$j++){
//    			echo $dm[$i]['rate']." ".$dm[$j]['rate']." ";
    			if (($dm[$j]['rate'])>($dm[$i]['rate'])){
//    				echo $j."-".$i."-".$dm[$j]['rate']."-".$dm[$i]['$rate']."<br />";
    				$temp = $dm[$i];
    				$dm[$i] = $dm[$j];
    				$dm[$j] = $temp;
    			}
    		}
    	}
 	   	$this->view->fmk_list = $people;
 	   	$this->view->isfriend_arr = $isfriend;
 	   	$this->view->dm = $dm;
	}
	
	public function requestAction(){
		FriendrequestModel::request($this->info_viewer['id'],$this->info_username['id']);
	}
	
	public function acceptfriendAction(){
		$request_id = $this->_request->getParam('request_id');
		$info = FriendrequestModel::getrequestinfo($request_id);
		FriendrequestModel::deleterequest($request_id);
		FriendModel::addfriend($info['from_id'],$info['to_id']);
	}
	
	public function refusefriendAction(){
		$request_id = $this->_request->getParam('request_id');
		FriendrequestModel::deleterequest($request_id);
	}
	
	public function searchAction(){
		$layout = $this->_helper->layout();
		$layout->disableLayout();
		$key = $this->_request->getParam('key');
		$list = UserModel::search($key);
		$this->view->list_friend = $list;
	}
} 